//
//  MainViewController.swift
//  HMWeibo
//
//  Created by wulei on 15/12/6.
//  Copyright © 2015年 wulei. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    // MARK: - 监听方法
  @objc private func clickComposedButton() {
        print("点我了")
    var VC: UIViewController
    if HMUserAccountViewModel.sharedUserAccountViewModel.isLogon {
        VC = ComposeViewController()
    }else{
        VC = HMOAuthViewController()
    }
    
    let nav = UINavigationController(rootViewController: VC)
    presentViewController(nav, animated: true, completion: nil)
    }
    
    // MARK: - 生命周期函数
    override func viewDidLoad() {
        super.viewDidLoad()

        addChildViewControllers()
        setComposedButton()
    }

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        tabBar.bringSubviewToFront(composedButton)
    }
    
    
    // MARK: - 懒加载控件
    private lazy var composedButton: UIButton = UIButton(imageName: "tabbar_compose_icon_add", backImageName: "tabbar_compose_button")


}


// MARK: - 设置界面

extension MainViewController {
    
    
    func setComposedButton(){
        tabBar.addSubview(composedButton)
        let count = childViewControllers.count
        let w = tabBar.bounds.width / CGFloat(count) - 1
        composedButton.frame = CGRectInset(tabBar.bounds, w * 2, 0)
        
        composedButton.addTarget(self, action: "clickComposedButton", forControlEvents: .TouchUpInside)
        
    }

    
    
    
    

    func addChildViewControllers() {
        
        tabBar.tintColor = UIColor.orangeColor()
        addChildViewController(HomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(MessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        
        addChildViewController(UIViewController())
        
        addChildViewController(DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(profileTableViewController(), title: "我", imageName: "tabbar_profile")
    }
    
    
    
    
    func addChildViewController(VC: UIViewController, title: String, imageName: String) {
        

        VC.title = title
        VC.tabBarItem.image = UIImage(named: imageName)
        let Nav = UINavigationController(rootViewController: VC)
        
        addChildViewController(Nav)
        
        
    }
}